#pragma once
#include <stdio.h>
#include <string>
#include "log.hpp"
#include "init.hpp"

// enum
// {
//     DIVZERO = 1,
//     UNKNOW
// };

extern Log log;
Dict dict;
struct task
{
public:
   task(int fd, string ip, uint16_t port)
   {
       _fd = fd;
       _ip = ip;
       _port = port;
   }

   void run()
   {
    //常服务在线程池跑不合理,循环
       cout << "welcome " << _ip << endl;
       char message[1024];

       ssize_t n = read(_fd, message, sizeof(message) - 1);
       if (n > 0)
       {
           message[n] = 0;
           // 数据处理
           string echo_string;
        //    echo_string = "[" + _ip + ":" + to_string(_port) + "]: " + message;
        //    cout << echo_string << endl;
           echo_string = dict.translate(message);

           write(_fd, echo_string.c_str(), echo_string.size());
       }
       else if (n == 0)
       {
           log.logmessage(info, "quit server");
       }
       else
       {
           log.logmessage(warning, "read error");
       }

       close(_fd);
   }

private:
    int _fd;
    string _ip;
    uint16_t _port;
};